package kr.ac.hufs;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

public class FileTransferManager {

	DataOutputStream out;
	DataInputStream in;


	public void sendFile(Socket dataSock, UserInfoProvider uip, int asciiMode) {// get

		byte[] tByte = new byte[50000];
		char[] tChar = new char[50000];
		int n;
		String path =  uip.getCli_fileName();

		int i = 0;

		try {
			File file = new File(path);
			if (!file.exists()) {
				//return "450 Requested file action not taken.";
			}

			if (var.BINARY_MODE == var.ON) {
				//Log.e("Binary Mode", "Binary Mode");
				DataInputStream dis = new DataInputStream(new FileInputStream(
						new File(path)));
				DataOutputStream dos = new DataOutputStream(
						dataSock.getOutputStream());

				while ((n = dis.read(tByte)) != -1) {
					//Log.e("byte length", n + "");
					dos.write(tByte, 0, n);
					dos.flush();
					//System.err.println("this is binary1");
				}
				System.err.println("this is binary1");
				//Log.e("last byte length", tByte.length + "");

				dis.close();
				dos.close();

			} else if (var.BINARY_MODE == var.OFF) {
				//Log.e("ascii Mode", "ascii Mode");
				String temp = "";
				BufferedReader br = new BufferedReader(new FileReader(path));
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
						dataSock.getOutputStream(), "EUC-KR"));

				while ((n = br.read(tChar)) != -1) {
				//	Log.e("char length", n + "");
					bw.write(tChar, 0, n);
					bw.flush();
				}

				//Log.e("last char length", n + "");

				br.close();
				bw.close();
				System.err.println("this is ascii2");
			}
			

		} catch (FileNotFoundException e) {
			//return "450 Requested file action not taken.";
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			//return "421 Service not available, closing control connection.";
		}
		//return "226 file transfer. OK";
	}

	
	
	
	public void strFile(Socket dataSock, UserInfoProvider uip, int asciiMode) {// put

		byte[] tByte = new byte[50000];
		int n;
		char[] tChar = new char[50000];
		String path = uip.getSrv_filename();
		

		try {
			File file = new File(path);
			if (!file.exists()) {
				file.createNewFile();
			}

			if (var.BINARY_MODE == var.ON) {
				//Log.e("Binary Mode", "Binary Mode");
				DataOutputStream dos = new DataOutputStream(
						new FileOutputStream(new File(path)));
				DataInputStream dis = new DataInputStream(
						dataSock.getInputStream());

				while ((n = dis.read(tByte)) != -1) {
				//	Log.e("char length", n + "");
					dos.write(tByte, 0, n);
					dos.flush();
				}
				dis.close();
				dos.close();
				System.err.println("this is binary3");
			} else if (var.BINARY_MODE == var.OFF) {
				//Log.e("ascii Mode", "ascii Mode");

				BufferedReader br = new BufferedReader(new InputStreamReader(
						dataSock.getInputStream(), "EUC-KR"));
				BufferedWriter bw = new BufferedWriter(new FileWriter(path));
				while ((n = br.read(tChar)) != -1) {
					//Log.e("char length", n + "");
					//Log.e("char length", n + "");
					bw.write(tChar, 0, n);
					bw.flush();
					//System.err.println("this is ascii4");

				}
				System.err.println("this is ascii4");
				bw.close();
				br.close();

			}

		} catch (FileNotFoundException e) {
			//return "450 Requested file action not taken.";
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			//return "421 Service not available, closing control connection.";
		}
		//return "226 file transfer. OK";
	}

}
